/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVK;

import DAO.LoaiSanPhamDao;
import DAO.SanPhamDao;
import GUI.MainForm;
import POJO.LoaiSanPham;
import POJO.SanPham;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Khoi
 */
public class frLapBaoCaoHangCanNhap extends javax.swing.JInternalFrame {

	/**
	 * Creates new form frLapBaoCaoHangCanNhap
	 */
	private DefaultTableModel tbmodel;
	private DefaultComboBoxModel cbModel;

	public frLapBaoCaoHangCanNhap() {
		initComponents();
		this.tbmodel = new DefaultTableModel();
		this.cbModel = new DefaultComboBoxModel();
		load();
		this.jComboBox1.setModel(cbModel);

		loadCobo();

	}

	private void loadCobo() {

		LoaiSanPhamDao dao = new LoaiSanPhamDao();

		ArrayList<LoaiSanPham> ds = dao.layDanhSach();
		LoaiSanPham tmp = new LoaiSanPham(0, "ALL");
		this.cbModel.addElement(tmp);
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel.addElement(ds.get(i));
		}

	}

	public void DefaultFrom() {
		tbmodel = new DefaultTableModel();
		// tao cac cot
		this.tbmodel.addColumn("STT ");
		this.tbmodel.addColumn("Mã SP");
		this.tbmodel.addColumn("Tên sản phẩm");
		this.tbmodel.addColumn("Số lượng tồn ");
		this.tbmodel.addColumn("Số lượng tồn tối thiểu");
	}

	private void load() {
		DefaultFrom();
		ArrayList<SanPham> ds = new ArrayList<SanPham>();
		try {
			ds = SanPhamDao.layDanhSachSanPhamCanNhap();

		} catch (Exception ex) {
			Logger.getLogger(SanPhamDao.class.getName()).log(Level.SEVERE, null, ex);

		}

		//	String[] columns = new String[]{"Mã NV", "Ho"};
		int i = 1;

		for (SanPham sp : ds) {
			Object[] items = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				sp.getSlTon(),
				sp.getSlTonToiThieu()};
			i++;
			tbmodel.addRow(items);
		}

		jTable1.setModel(tbmodel);
		// canh deu the ti le
		int[] va = {10, 10, 40, 20, 20};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = jTable1.getWidth() * va[l] / 100;
			jTable1.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
	}

	/**
	 *
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel5 = new javax.swing.JLabel();
              jPanel2 = new javax.swing.JPanel();
              jComboBox1 = new javax.swing.JComboBox();
              jLabel1 = new javax.swing.JLabel();
              jButton5 = new javax.swing.JButton();
              jPanel3 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              jTable1 = new javax.swing.JTable();
              jPanel5 = new javax.swing.JPanel();
              jButton1 = new javax.swing.JButton();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel5.setText("LẬP BÁO CÁO CÁC MẶT HÀNG CẦN NHẬP");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Item 2", "Item 3", "Item 4" }));
              jComboBox1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jComboBox1ActionPerformed(evt);
                     }
              });

              jLabel1.setText("Loại sản phẩm:");

              jButton5.setText("Lập báo cáo");
              jButton5.setToolTipText("gọi lập phiếu nhập kho");
              jButton5.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton5ActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(169, 169, 169)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jComboBox1, 0, 140, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5)
                            .addGap(166, 166, 166))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel1)
                                   .addComponent(jButton5))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách mặt hàng cần nhập:"));
              jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

              jTable1.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã sản phẩm", "Tên sản phẩm", "Số lượng tồn", "Tồn tối thiểu"
                     }
              ));
              jScrollPane1.setViewportView(jTable1);

              jPanel3.add(jScrollPane1);

              jButton1.setText("Nhập thêm sản phẩm");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
              jPanel5.setLayout(jPanel5Layout);
              jPanel5Layout.setHorizontalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1)
                            .addGap(265, 265, 265))
              );
              jPanel5Layout.setVerticalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jButton1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

       private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
		// TODO add your handling code here:
		DefaultFrom();
		LoaiSanPham lsp = (LoaiSanPham) cbModel.getSelectedItem();

		ArrayList<SanPham> ds = new ArrayList<SanPham>();
		ds = null;
		if (lsp.getMaLSP() == 0) {
			try {
				ds = SanPhamDao.layDanhSachSanPhamCanNhap();

			} catch (Exception ex) {
				Logger.getLogger(SanPhamDao.class.getName()).log(Level.SEVERE, null, ex);

			}
		} else {
			try {
				ds = SanPhamDao.layDanhSachSanPhamCanNhapByLoaiSanPham(lsp);

			} catch (Exception ex) {
				Logger.getLogger(SanPhamDao.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		int i = 1;
		for (SanPham sp : ds) {
			Object[] items = new Object[]{
				i,
				sp.getMaSP(),
				sp.getTenSP(),
				sp.getSlTon(),
				sp.getSlTonToiThieu()};
			i++;
			tbmodel.addRow(items);
		}

		jTable1.setModel(tbmodel);
		// canh deu the ti le
		int[] va = {10, 10, 40, 20, 20};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = jTable1.getWidth() * va[l] / 100;
			jTable1.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
       }//GEN-LAST:event_jButton5ActionPerformed

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		frLapPhieuNhapKho fr = new frLapPhieuNhapKho();

		fr.setVisible(true);
		MainForm.jDesktopPane1.add(fr);
		try {
			fr.setSelected(isSelected);
		} catch (PropertyVetoException ex) {
			Logger.getLogger(frLapBaoCaoHangCanNhap.class.getName()).log(Level.SEVERE, null, ex);
		}
       }//GEN-LAST:event_jButton1ActionPerformed

       private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
              // TODO add your handling code here:
       }//GEN-LAST:event_jComboBox1ActionPerformed

	
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton jButton1;
       private javax.swing.JButton jButton5;
       private javax.swing.JComboBox jComboBox1;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel5;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JTable jTable1;
       // End of variables declaration//GEN-END:variables
}
